Skip to content

fix(mothership) fix row_count#3622

Merged
TheodoreSpeaks merged 1 commit intostagingfrom
fix/row-count-context
Mar 17, 2026
Merged

fix(mothership) fix row_count#3622
TheodoreSpeaks merged 1 commit intostagingfrom
fix/row-count-context

Conversation

@TheodoreSpeaks
Copy link
Collaborator

Summary

Row count previously depended on row count property that was never updated. Changed to use sql count for accuracy

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Other: ___________

Testing

Validated row count is correctly passed via context to mothership.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

Screenshots/Videos

@cursor
Copy link

cursor bot commented Mar 17, 2026

PR Summary

Low Risk
Low risk, localized query change that only affects how rowCount is computed when fetching a table; main concern is potential query/perf differences due to the new join/grouping.

Overview
Fixes incorrect table rowCount when fetching a single table by switching getTableById to compute the count directly in SQL.

getTableById now selects explicit table fields, LEFT JOINs user_table_rows, and uses count() + groupBy to return an accurate rowCount (including 0 for empty tables), instead of relying on a stale/unmaintained value.

Written by Cursor Bugbot for commit 91fad78. This will update automatically on new commits. Configure here.

@vercel
Copy link

vercel bot commented Mar 17, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Mar 17, 2026 5:23am

Request Review

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 17, 2026

Greptile Summary

This PR fixes a bug in getTableById where rowCount was sourced from a stored property on the table definition that was never updated, causing it to always be stale. The fix switches to computing rowCount dynamically via a SQL COUNT aggregate over userTableRows, using a LEFT JOIN and GROUP BY, which is identical to the pattern already used in the listTables function.

Key changes:

  • getTableById now uses an explicit field projection (instead of SELECT *) to selectively select columns from userTableDefinitions alongside the computed rowCount
  • A LEFT JOIN on userTableRows is added so that tables with zero rows still appear in the result
  • GROUP BY userTableDefinitions.id is added to support the COUNT aggregate
  • rowCount is now COALESCE(COUNT(userTableRows.id), 0), matching the existing pattern in listTables

Confidence Score: 5/5

  • This PR is safe to merge — it's a targeted, correct bug fix with no regressions introduced.
  • The change is minimal and well-scoped: it replaces a stale stored property with a live SQL aggregate, using the exact same pattern already validated in listTables. The LEFT JOIN correctly handles the zero-row case, GROUP BY is required for the aggregate, and LIMIT 1 is preserved. No types or interfaces were changed, and the return shape of TableDefinition is fully satisfied.
  • No files require special attention.

Important Files Changed

Filename Overview
apps/sim/lib/table/service.ts Fixes getTableById to compute rowCount via a SQL COUNT on userTableRows using a LEFT JOIN + GROUP BY, instead of relying on a stale stored property. The approach is consistent with the existing listTables function.

Sequence Diagram

sequenceDiagram
    participant Caller
    participant getTableById
    participant DB as Database

    Caller->>getTableById: getTableById(tableId, options)
    getTableById->>DB: SELECT id, name, ..., COALESCE(COUNT(rows.id), 0) AS rowCount<br/>FROM userTableDefinitions<br/>LEFT JOIN userTableRows ON rows.tableId = defs.id<br/>WHERE defs.id = tableId [AND archivedAt IS NULL]<br/>GROUP BY defs.id<br/>LIMIT 1
    DB-->>getTableById: result row with computed rowCount
    getTableById-->>Caller: TableDefinition (with accurate rowCount)
Loading

Last reviewed commit: 91fad78

@TheodoreSpeaks TheodoreSpeaks merged commit 2a7b07e into staging Mar 17, 2026
12 checks passed
@TheodoreSpeaks TheodoreSpeaks deleted the fix/row-count-context branch March 17, 2026 05:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant